const { query } = require('./db');
const { addLikeCount, reduceLikeCount } = require('./article');

async function exitsLike(postId, userId) {
    const sql = 'select * from likes WHERE post_id =? AND user_id =?';
    const result = await query(sql, [postId, userId]);
    if (result && result.length > 0)
        return true;
    return false;
}

async function addLike(postId, userId) {
    const sql = 'INSERT INTO likes (post_id, user_id) VALUES (?,?)';
    const result = await query(sql, [postId, userId]);
    if (result.affectedRows > 0) {
        await addLikeCount(postId);
    }
    return result;
}

async function deleteLike(postId, userId) {
    const sql = 'DELETE FROM likes WHERE post_id =? AND user_id =?';
    const result = await query(sql, [postId, userId]);
    if (result.affectedRows > 0) {
        await reduceLikeCount(postId);
    }
    return result;
}

module.exports = {
    exitsLike,
    addLike,
    deleteLike,
};

